<?php
	include_once 'isLogin.php';
	// $script_name = explode("/", $_SERVER['SCRIPT_NAME']);
	// $script_name = $script_name[count($script_name)-1];
	// $smarty->assign("script_name",$script_name);

	$servicec=new service(); //服务类
	$table_name = "zs_service_class"; //分类表
	$key_index = array('class_name','upid');


	//限制级别
	if($_GET['level']!='' && $_GET['level']!=null){
		$level=$_GET['level']+1;
	}else{
		$level=0;
	}
	$smarty->assign("level",$level);


	//删除
	if($_GET['action']=='del'){
		//删除一级
		$commonc->del($table_name,$_POST['id']);
		//删除二级
		echo $commonc->del($table_name,$_POST['id'],'','upid');
		exit();
	}


   //添加
	if($_GET['action']=='add'){
		//获取上级级别
		$up = $commonc->getInfo($table_name,$_GET['upid']);

		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		//子级分类
		if($_GET['upid'] != null && $_GET['upid'] != ""){
			$arr['upid'] = $_GET['upid'];
			$arr['level'] = $level;
		}

		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//查询记录
		$data = $commonc->getInfo($table_name,$id);
	
		if($level<2){
			$child='<a href="service_class_list.php?upid='.$id.'&level='.$level.'">子类</a>';
		}
		
		if($id!=''){
				//插入分类模板
				$return='<tr>
				<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
				<td value_id="'.$id.'">'.$data['class_name'].'</td>
				<td class="xiugai">
				  	<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\'service_class_list.php?action=edit\')">修改</a>
				  	'.$child.'
				  </td>
				</tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}

		echo json_encode($arr);
		exit();
	}

	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}


	//得到所有内容 
	$where='';
	if($_GET['upid'] != null && $_GET['upid']!=""){
		$where .= " AND c.upid=".$_GET['upid'];
		$smarty->assign("upid",$_GET['upid']);
	}else{
		$where .= " AND c.upid=0";
	}
	//搜索
	if($_POST){
		$where.=" AND c.class_name like '%".$_POST['title']."%'";
		$smarty->assign('search',$_POST);
	}

	$row=$servicec->service_class_list($where);
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);

	$smarty->display("service_class_list.html");
?>